//
// request_handler.cpp
// ~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include "request_handler.hpp"
#include <fstream>
#include <sstream>
#include <string>
#include "log.h"


//----------------------------------Need changes----------------
#include <boost/shared_ptr.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
typedef boost::shared_ptr<boost::interprocess::message_queue> MsgQueuePtr ;
extern MsgQueuePtr pMsgQueue ;


request_handler::request_handler()
{
}

void request_handler::handle_request(const Message& inMsg, Message & outRep)
{
    using namespace std ;

    stock::Favorite favStock ;

    if ( (favStock.ParseFromArray(inMsg.body(),inMsg.body_length())) == true)
    {
        if ( favStock.has_type() )
        {
            LOGCON.infoStream() << "type:" << favStock.type() << "\n" ;
        }
        if ( favStock.has_name() )
        {
            LOGCON.infoStream() << "name:" << favStock.name() << "\n" ;
        }
        if ( favStock.has_market() )
        {
            LOGCON.infoStream() << "market:" << (int)(favStock.market().type()) << "\n" ;
        }
        if ( favStock.has_code() )
        {
            LOGCON.infoStream() << "code:" << favStock.code() << "\n" ;
        }
        if ( favStock.has_description() )
        {
            LOGCON.infoStream() << "description:" << favStock.description() << "\n" ;
        }
    }
    std::memcpy(outRep.body(), inMsg.body(), inMsg.body_length()) ;
    outRep.body_length(inMsg.body_length()) ;
    outRep.encode_header() ;
    pMsgQueue->send(inMsg.data(), inMsg.length(), 0) ;
}
